package com.wn.class30.cotroller.weixin;

import com.wn.class30.service.VipService;
import com.wn.class30.tools.OptionidUtils;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.UseridUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@RestController
@Api(value = "微信菜单接口",tags = "微信菜单接口")
@RequestMapping(value = "/weixin")
@Validated
public class RedirectController {
    @Resource
    private OptionidUtils optionidUtils;

    @Resource
    private VipService vipService;
    @Resource
    private UseridUtils useridUtils;
    @GetMapping(value = "/queryIndex")
    @ApiOperation(value = "用户个人中心", notes = "用户个人中心")
    public Result queryIndex(HttpServletResponse resp, HttpServletRequest req) throws Exception{
        String url=req.getScheme()+"://"+ req.getServerName()+"/weixin/queryIndex";
        System.out.println(url+"url");
        boolean b = optionidUtils.validateOAuthOpenId(req, resp, url);
        String url1 ="/weixin/user/index.html";
        if(b==true){
            Integer userid = useridUtils.queryUseridByOptenid(req);
            if(userid!=null){
            vipService.updateVipLv(userid);}
            resp.sendRedirect(url1);
            return null;
        }else{
            return null;
        }
    }

    @GetMapping(value = "/shoppingMall")
    @ApiOperation(value = "商城首页接口", notes = "商城首页接口")
    public Result shoppingMall(HttpServletResponse resp, HttpServletRequest req) throws Exception{
        String url=req.getScheme()+"://"+ req.getServerName()+"/weixin/shoppingMall";
        boolean b = optionidUtils.validateOAuthOpenId(req, resp, url);
        String url1 ="/weixin/shoppingmall/index.html";
        if(b==true){
            Integer userid = useridUtils.queryUseridByOptenid(req);
            if(userid!=null){
                vipService.updateVipLv(userid);}
            resp.sendRedirect(url1);
            return null;
        }else{
            return null;
        }
    }

    @GetMapping(value = "/jiaMeng")
    @ApiOperation(value = "微信公众号加盟", notes = "微信公众号加盟")
    public Result jiaMeng(HttpServletResponse resp, HttpServletRequest req) throws Exception{
        String url=req.getScheme()+"://"+ req.getServerName()+"/weixin/jiaMeng";
        boolean b = optionidUtils.validateOAuthOpenId(req, resp, url);
        String url1 ="/weixin/user/joinIndex.html";
        if(b==true){
            Integer userid = useridUtils.queryUseridByOptenid(req);
            if(userid!=null){
                vipService.updateVipLv(userid);}
            resp.sendRedirect(url1);
            return null;
        }else{
            return null;
        }
    }



    @GetMapping(value = "/wexinPay")
    @ApiOperation(value = "微信支付接口", notes = "微信支付接口")
    public Result WexinPay(HttpServletResponse resp, HttpServletRequest req,Integer bussinessId) throws Exception{
        if(req.getSession().getAttribute("wxpaybusinessid")==null){
            req.getSession().setAttribute("wxpaybusinessid",bussinessId);
        }
        String url=req.getScheme()+"://"+ req.getServerName()+"/weixin/wexinPay";
        boolean b = optionidUtils.validateOAuthOpenId(req, resp, url);
        String url1 ="/weixin/pay/xianxiapay.html";
        if(b==true){
            Integer userid = useridUtils.queryUseridByOptenid(req);
            if(userid!=null){
                vipService.updateVipLv(userid);}
            resp.sendRedirect(url1);
            return null;
        }else{
            return null;
        }
    }

}
